home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-desktop-9.10-i386-PL.iso / casper / filesystem.squashfs / usr / share / ibus / setup / icon.py < prev    next >
Encoding:
Python Source  |  2009-11-05  |  1.7 KB  |  61 lines

  1. # vim:set et sts=4 sw=4:
  2. #
  3. # ibus - The Input Bus
  4. #
  5. # Copyright (c) 2007-2008 Huang Peng <shawn.p.huang@gmail.com>
  6. #
  7. # This library is free software; you can redistribute it and/or
  8. # modify it under the terms of the GNU Lesser General Public
  9. # License as published by the Free Software Foundation; either
  10. # version 2 of the License, or (at your option) any later version.
  11. #
  12. # This library is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Lesser General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Lesser General Public
  18. # License along with this program; if not, write to the
  19. # Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20. # Boston, MA  02111-1307  USA
  21.  
  22. __all__ = (
  23.     "load_icon"
  24. )
  25.  
  26. import gtk
  27. from gtk import gdk
  28. from os import path
  29.  
  30.  
  31. icon_theme = gtk.icon_theme_get_default()
  32. dir = path.dirname(__file__)
  33. icondir = path.join(dir, "..", "icons")
  34. icon_theme.prepend_search_path(icondir)
  35.  
  36. icon_cache = {}
  37.  
  38. def load_icon(icon, size):
  39.     if (icon, size) in icon_cache:
  40.         return icon_cache[(icon, size)]
  41.  
  42.     icon_size = gtk.icon_size_lookup(size)[0]
  43.     pixbuf = None
  44.     try:
  45.         pixbuf = gdk.pixbuf_new_from_file(icon)
  46.         w, h = pixbuf.get_width(), pixbuf.get_height()
  47.         rate = max(w, h) / float(icon_size)
  48.         w = int(w / rate)
  49.         h = int(h / rate)
  50.         pixbuf = pixbuf.scale_simple(w, h, gdk.INTERP_BILINEAR)
  51.     except:
  52.         pass
  53.     if pixbuf == None:
  54.         try:
  55.             theme = gtk.icon_theme_get_default()
  56.             pixbuf = theme.load_icon(icon, icon_size, 0)
  57.         except:
  58.             pass
  59.     icon_cache[(icon, size)] = pixbuf
  60.     return pixbuf
  61.